package com.cefa.finance.service;

import com.cefa.finance.dao.*;
import com.cefa.finance.model.*;
import com.cefa.finance.result.ResultModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class UserCreditInfoService {

    private final static Logger logger = LoggerFactory.getLogger(UserCreditInfoService.class);

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private VehicleRepository vehicleRepository;

    @Autowired
    private ContractRepository contractRepository;

    @Autowired
    private AccidentRecordRepository accidentRecordRepository;

    @Autowired
    private MaintenanceRecordRepository maintenanceRecordRepository;

    @Autowired
    private RepaymentRecordRepository repaymentRecordRepository;

    @Autowired
    private ContractMapper contractMapper;

    public ResultModel listuserCreditInfo(String searchText, Integer pageNo, Integer pageCount) {

        ArrayList<Contract> allResult = new ArrayList<>();
        ArrayList<Contract> certainContracts = new ArrayList<>();
        HashMap<String, Object> resultMap = new HashMap<>();

        Iterable<Contract> allContracts = contractRepository.findAll();
        for (Contract contract : allContracts) {

            allResult.add(contract);

            if (searchText != null && !"".equals(searchText) && contract.getTenantryName() != null && contract.getVin() != null && contract.getPhone() != null) {
                if (contract.getTenantryName().contains(searchText) || contract.getVin().contains(searchText)) {
                    certainContracts.add(contract);
                }
            }
        }

        if (searchText != null && !"".equals(searchText)) {
            allResult = certainContracts;
        }

        if (pageNo != null && pageCount != null) {
            resultMap.put("totalPage", (allResult.size() - 1) / pageCount + 1);
            allResult = new ArrayList<>(allResult.subList((pageNo - 1) * pageCount, pageCount * pageNo > allResult.size() ? allResult.size() : pageCount * pageNo));
        }
        resultMap.put("contracts", allResult);
        return ResultModel.OK(resultMap);
    }



    public ResultModel listuserCreditInfo2(String searchText, Integer pageNo, Integer pageCount) {

        ArrayList<ContractModel> allResult = new ArrayList<>();
        ArrayList<ContractModel> certainContracts = new ArrayList<>();
        HashMap<String, Object> resultMap = new HashMap<>();

        List<ContractModel> allContracts = contractMapper.findAllContracts();
        for (ContractModel contract : allContracts) {

            allResult.add(contract);

            if (searchText != null && !"".equals(searchText) && contract.getTenantryName() != null && contract.getVin() != null && contract.getPhone() != null) {
                if (contract.getTenantryName().contains(searchText) || contract.getVin().contains(searchText)) {
                    certainContracts.add(contract);
                }
            }
        }

        if (searchText != null && !"".equals(searchText)) {
            allResult = certainContracts;
        }

        if (pageNo != null && pageCount != null) {
            resultMap.put("totalPage", (allResult.size() - 1) / pageCount + 1);
            allResult = new ArrayList<>(allResult.subList((pageNo - 1) * pageCount, pageCount * pageNo > allResult.size() ? allResult.size() : pageCount * pageNo));
        }
        resultMap.put("contracts", allResult);
        return ResultModel.OK(resultMap);
    }

    public ResultModel listCertainUserCreditInfo(Contract one) {
        Contract contract = contractRepository.findOne(one.getId());
        Vehicle vehicle = vehicleRepository.findOne(contract.getVehicleId());
        List<AccidentRecord> accidentRecords = accidentRecordRepository.findByUserIdAndVehicleId(contract.getUserId(), contract.getVehicleId());
        List<MaintenanceRecord> maintenanceRecords = maintenanceRecordRepository.findByUserIdAndVehicleId(contract.getUserId(), contract.getVehicleId());
        List<RepaymentRecord> repaymentRecords = repaymentRecordRepository.findByUserIdAndVehicleId(contract.getUserId(), contract.getVehicleId());
        User user = userRepository.findOne(contract.getUserId());
        contract.setUser(user);
        vehicle.setRepaymentRecords(repaymentRecords);
        vehicle.setAccidentRecords(accidentRecords);
        vehicle.setMaintenanceRecords(maintenanceRecords);
        contract.setVehicle(vehicle);
        contract.setMaintenanceNum(maintenanceRecords.size());
        contract.setAccidentNum(accidentRecords.size());
        contract.setRepaymentNum(repaymentRecords.size());
        return ResultModel.OK(contract);
    }
}

